package cn.itcast.web.controller.factory;
import cn.itcast.domain.cargo.Contract;
import cn.itcast.domain.cargo.Factory;
import cn.itcast.domain.cargo.FactoryExample;
import cn.itcast.service.cargo.FactoryService;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;

@Controller
@RequestMapping("/baseinfo/changjia")
@Slf4j
public class FactoryController extends BaseController {
    @Reference
    private FactoryService factoryService;

    @RequestMapping("/list")
    public String list(@RequestParam(defaultValue = "1") Integer pageNum,
                       @RequestParam(defaultValue = "5") Integer pageSize){
        // 构造条件对象：根据企业id查询、创建时间降序排序
        FactoryExample example = new FactoryExample();
        // 降序排序
        example.setOrderByClause("create_time desc");
        FactoryExample.Criteria criteria = example.createCriteria();
        // 显示状态位1的购销合同
        criteria.andStateEqualTo(String.valueOf(1));

        // 分页查询
        PageInfo<Factory> pageInfo = factoryService.findByPage(example, pageNum, pageSize);

        request.setAttribute("pageInfo", pageInfo);
        return "baseinfo/changjia-list";
    }


    /**
     * 进入添加页面
     */
    @RequestMapping("/toAdd")
    public String toAdd() {
        return "baseinfo/changjia-add";
    }


    /**
     * 添加或修改
     */
    @RequestMapping("/edit")
    public String edit(Factory factory) {
        // 设置企业信息（从登陆用户中获取）
        factory.setCompanyId(getLoginCompanyId());
        factory.setCompanyName(getLoginCompanyName());

        // 判断：根据id是否为空判断执行添加还是修改
        if (StringUtils.isEmpty(factory.getId())) {
            // 设置创建时间、创建人、创建人的部门
            factory.setCreateTime(new Date());
            factory.setUpdateTime(new Date());
            factory.setCreateBy(getLoginUser().getId());
            factory.setCreateDept(getLoginUser().getDeptId());
            // 添加
            factoryService.save(factory);
        } else {
            // 修改
            factoryService.update(factory);
        }

        return "redirect:/baseinfo/changjia/list";
    }

    /**
     * 进入修改页面
     */
    @RequestMapping("/toUpdate")
    public String toUpdate(String id, Model model) {
        // 根据id查询
        Factory factory = factoryService.findById(id);
        model.addAttribute("factory", factory);
        return "baseinfo/changjia-update";
    }


    /**
     * 删除
     */
    @RequestMapping("/delete")
    public String delete(String id) {
        factoryService.delete(id);
        return "redirect:/baseinfo/changjia/list";
    }

    /**
     * 查看
     */
    @RequestMapping("/toView")
    public String toView(String id) {
        //        // 根据id查询
        Factory factory = factoryService.findById(id);
        request.setAttribute("factory",factory);
        return "baseinfo/changjia-view";
    }

    @RequestMapping("/submit")
    public String submit(String id) {
        // 提交：修改购销合同状态改为1
        Factory factory  = new Factory();
        // 设置修改条件
        factory.setId(id);
        // 设置修改的状态值
        factory.setState(1);
        // 修改 (动态sql，有选择的修改)
        factoryService.update(factory);
        return "redirect:/baseinfo/changjia/list";
    }
}
